﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Transactions;
using Core.Contract.DataContract;
using Data.BO;
using www.Models;

namespace Controllerss
{
    public class MenuController
    {
        #region Value
        private readonly MenuRefBO _menuBo = new MenuRefBO();
        private readonly MenuValueBO _menuValueBo = new MenuValueBO();
        #endregion

        #region Ham Lay du lieu

        public IQueryable<RelaxMenuDataContract> GetMenuDataContracts()
        {
            using (var scope = new TransactionScope())
            {
                var lReturn = _menuBo.GetAllMenuDataContracts();
                scope.Complete();
                return lReturn;
            }
        }
        #endregion

        #region Add

        public long Add(List<RelaxMenuDataContract> menuDataContract)
        {
            using (var scope = new TransactionScope())
            {
                long iD = 0;
                var menu = new MENU_REF
                {
                    parent = menuDataContract[0].Parent,
                    link = menuDataContract[0].Link,
                    target = menuDataContract[0].Target,
                    ord = menuDataContract[0].Ord,
                    active = menuDataContract[0].Active,
                    type = menuDataContract[0].Type
                };
                if (menuDataContract[0].CategoryId > 0)
                    menu.categoryid = menuDataContract[0].CategoryId;
                _menuBo.Add(menu);
                var first = _menuBo.GetMenuId().OrderByDescending(o => o.Id).FirstOrDefault();
                if (first != null)
                {
                    iD = first.Id;
                    foreach (var item in menuDataContract)
                    {
                        var menuValue = new MENU_VALUE
                        {
                            menuid = iD,
                            languagecode = item.Languagecode,
                            name = item.Name,
                            tag = item.Tag,
                            keyword = item.Keyword,
                            decription = item.Decription,
                        };
                        _menuValueBo.Add(menuValue);
                    }
                }

                scope.Complete();
                return iD;
            }
        }
        #endregion

        #region Update
        public Boolean Update(List<RelaxMenuDataContract> menuDataContract)
        {
            using (var scope = new TransactionScope())
            {
                var menu = new MENU_REF
                {
                    id = menuDataContract[0].Id,
                    parent = menuDataContract[0].Parent,
                    link = menuDataContract[0].Link,
                    target = menuDataContract[0].Target,
                    ord = menuDataContract[0].Ord,
                    active = menuDataContract[0].Active,
                    type = menuDataContract[0].Type
                };
                if (menuDataContract[0].CategoryId > 0)
                    menu.categoryid = menuDataContract[0].CategoryId;

                _menuBo.Update(menu);
              
                foreach (var item in menuDataContract)
                {
                    var menuvalue = new MENU_VALUE
                    {
                        menuid = item.Id,
                        languagecode = item.Languagecode,
                        name = item.Name,
                        tag = item.Tag,
                        keyword = item.Keyword,
                        decription = item.Decription
                    };
                    if (!_menuValueBo.Update(menuvalue))
                    {
                        _menuValueBo.Add(menuvalue);
                    }
                }
                scope.Complete();
                return true;
            }
        }
        #endregion

        #region Delete

        public Boolean Delete(List<long> lId, long iLanguageCode)
        {
            using (var scope = new TransactionScope())
            {
                foreach (var id in lId)
                {
                    _menuValueBo.Delete(id, iLanguageCode);
                }
                scope.Complete();
                return true;
            }
        }
        #endregion
    }
}
